package com.cssl.message;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cssl.entity.ProductDetails;
import com.cssl.entity.ProductOrder;
import com.cssl.service.ProductDetailsService;
import com.cssl.service.ProductOrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

@Component
public class MessageConsumer {

    @Autowired
    private ProductOrderService pos; //商品订单
    @Autowired
    private ProductDetailsService pds; //商品订单详情信息

    @Autowired
    private IMessage message; //发送消息接口

    /**
     * 直接接收延时消息 判断该订单是否付款？
     */
    @RabbitListener(queues = {"delayQueue"})
    public void receiveDelay(Map<String, Object> messageMap){
        String msg = (String) messageMap.get("msg");
        Map<Integer, Integer> map = (Map<Integer, Integer>) messageMap.get("map");

        ProductOrder orderProduct = pos.getById(msg);
        if(orderProduct.getStatus() == 0){
            //商品未付款 删除该订单 和订单下的商品详情
            //删除商品订单详情信息
            if(pds.remove(new QueryWrapper<ProductDetails>().eq("po_id", msg))) {
                System.out.println("已删除订单商品详情信息：" + msg);
                //删除商品订单
                if(pos.removeById(msg))
                    System.out.println("已删除订单："+msg);
                else
                    System.out.println("订单删除失败："+msg);
            }else{
                System.out.println("删除失败订单商品详情信息："+msg);
            }
            //发送消息回滚扣减库存
            message.HgReduceInventory(map);
        }
        System.out.println("当前时间为："+LocalDateTime.now());
        System.out.println("delayQueue:"+msg);
    }

}
